package com.eric.config;

import com.eric.resp.BaseResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 异常拦截器
 * @author: liuBing
 * @create: 2022-12-19 16:44
 **/

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class MyExceptionHandler {


    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Object validExceptionHandler(MethodArgumentNotValidException exception) {
        log.info("valid 校验失败 ： {}", exception.getMessage());
        String defaultMessage = exception.getBindingResult().getFieldErrors().stream().findFirst().get().getDefaultMessage();
        return new BaseResp<>(defaultMessage);
    }


}
